package com.coder.algorithm.offer_2;

import java.util.Arrays;

/**
 * 和为 s 的两个数字
 *
 * @author yuhushuan
 * @date 2020/7/1 11:21
 */
public class _57_1_TwoNumbersWithSum {
    private static void findNumbersWithSum(int[] data, int sum) {
        if (data == null || data.length < 2) {
            System.out.println("不存在");
            return;
        }
        int start = 0;
        int end = data.length - 1;
        int number1 = 0, number2 = 0;
        while (start < end) {
            int curSum = data[start] + data[end];
            if (curSum == sum) {
                number1 = data[start];
                number2 = data[end];
                break;
            } else if (curSum < 15) {
                start++;
            } else {
                end--;
            }
        }
        if (start < end) {
            System.out.println(number1 + ", " + number2);
        } else {
            System.out.println("不存在");
        }
    }

    public static void main(String[] args) {
        // 功能测试
        // 数组存在和 s
        print(new int[]{1, 2, 4, 7, 11, 15}, 15);
        // 数组不存在和 s
        print(new int[]{1, 2, 4, 7, 11, 15}, 0);
        print(new int[]{1, 2, 4, 7, 11, 15}, 30);

        // 特殊输入测试
        print(null, 1);
    }

    private static void print(int[] data, int sum) {
        System.out.println("数组：" + Arrays.toString(data));
        System.out.println("中和为：" + sum + " 两个数为：");
        findNumbersWithSum(data, sum);
        System.out.println();
    }
}
